package thread;

import controller.RightPanelController;
import javafx.application.Platform;
import utils.*;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by lincl on 2018/12/7.
 */
public class CopyThread implements Runnable {

    String versionStr;
    String callMethod;
    Object callObj;
    ACallBack callBack;

    public CopyThread(String versionStr, ACallBack callBack) {
        this.versionStr = versionStr;
        this.callBack = callBack;
    }

    @Override
    public void run() {
        String versions = StringUtils.trim(versionStr);
        if(!StringUtils.isNullOrEmpty(versions)){
            Set<Long> versionList = new HashSet<>();
            for(String s : versions.split(",")){
                for(String s1 : s.split("，")){
                    if(!StringUtils.isNullOrEmpty(s1)){
                        try {
                            versionList.add(Long.parseLong(s1));
                        }catch (Exception e){

                        }
                    }
                }
            }
            Long[] versionArr = versionList.toArray(new Long[versionList.size()]);
            if(versionArr.length>0){
                try {
                    ConfigBean bean = Global.getCurConfigBean();
                    SVNUtils.copyChangeList(versionArr, bean);
                    doCallBack(1);
                } catch (Exception e) {
                    e.printStackTrace();
                    doCallBack(-1);
                }
            } else {
                doCallBack(-2);
            }
        } else {
            doCallBack(-3);
        }
    }

    private void doCallBack(final int rs){
        try {
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        callBack.exe(rs);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
